# Single Color RGB565 Blob Tracking Example
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.

import sensor, image, time, math#导入此代码所依赖的模块

threshold_index = 0 # 0 for red, 1 for green, 2 for blue

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = (30, 100, -64, -8, -32, 32)# generic_green_thresholds     颜色阈值
              #(0, 30, 0, 64, -128, 0)  generic_blue_thresholds
              #(30, 100, 15, 127, 15, 127)  generic_red_thresholds
sensor.reset()#重置感光元件，重置摄像机
sensor.set_pixformat(sensor.RGB565)#设置颜色格式
sensor.set_framesize(sensor.QVGA)#图像大小
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.

while(True):
    clock.tick()
    img = sensor.snapshot()#截取感光元件图片
    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
        img.draw_rectangle(blob.rect())#方框圈住
        img.draw_cross(blob.cx(), blob.cy())#色块中心坐标
        # Note - the blob rotation is unique to 0-180 only.
       # print blod
      #  print（clock，fps()）
    for r in img.find_rects(threshold = 10000):
        img.draw_rectangle(r.rect(), color = (255, 0, 0))
    for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
    #    print("FPS %f" % clock.fps())
    #if blob==1 and r==1

